package com.qjj.studydainty.fragment;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qjj.studydainty.Db.DbUtil;
import com.qjj.studydainty.R;
import com.qjj.studydainty.adapter.HistoryAdapter;
import com.qjj.studydainty.entity.History;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.qjj.studydainty.entity.MyApplication.ACTIVITY_OBJECT;
import static com.qjj.studydainty.entity.MyApplication.OPEN_URL_IN_NEW_WINDOW;
import static com.qjj.studydainty.entity.MyApplication.handler;


public class HistoryFragment extends Fragment implements View.OnCreateContextMenuListener {

    private HistoryAdapter historyAdapter;
    private List<History> historyList = new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_history, container, false);
        RecyclerView recyclerView = view.findViewById(R.id.history_recycleView);

        initDBData();

        historyAdapter = new HistoryAdapter(getContext(), historyList);

        recyclerView.setAdapter(historyAdapter);
        //设置布局管理
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        //recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        //设置动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        //recyclerView.addItemDecoration(new SpaceItemDecoration(0, 30));
        //设置分割线
        recyclerView.addItemDecoration(new DividerItemDecoration(Objects.requireNonNull(getContext()), DividerItemDecoration.VERTICAL));//requireNonNull
        //recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.HORIZONTAL));

        //registerForContextMenu(recyclerView);

        historyAdapter.setOnItemClickListener(new HistoryAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                openUrl(position);
            }

            @Override
            public void OnItemLongClick(View view, int position) {
            }
        });

        recyclerView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
            @Override
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
                menu.setHeaderTitle("历史");
                Objects.requireNonNull(getActivity()).getMenuInflater().inflate(R.menu.history, menu);
            }
        });

        return view;

    }

    private void initDBData() {
        historyList = DbUtil.queryFromHistory(getContext());
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        int position = historyAdapter.getPosition();
        switch (item.getItemId()) {
            case R.id.open:
                openUrl(position);
                break;
            case R.id.delete:
                DbUtil.deleteByIdFromHistory(getActivity(), historyList.get(position).getId());
                historyAdapter.deleteData(position);
                break;
            case R.id.delete_all:
                DbUtil.deleteAllFromHistory(getActivity());
                historyAdapter.deleteAllData();
                break;
            case R.id.copy:
                copy(historyList.get(position).getUrl(), Objects.requireNonNull(getActivity()));
                break;
            default:
                return false;
        }
        return true;
    }

    private void openUrl(int position) {
        Message.obtain(handler, ACTIVITY_OBJECT, getActivity()).sendToTarget();
        Message.obtain(handler, OPEN_URL_IN_NEW_WINDOW, historyList.get(position).getUrl()).sendToTarget();
    }

    private void copy(String content, Context context) {
        ClipboardManager cmb = (ClipboardManager)
                context.getSystemService(Context.CLIPBOARD_SERVICE);
        if (cmb != null)
            cmb.setPrimaryClip(ClipData.newPlainText(null, content.trim()));
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        System.out.println("摧毁RecommendView");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("摧毁Recommend");
    }

}
